package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResCording1;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.kucai.yifaqapp.global.App.getContext;

public class RecrdActivity1 extends BaseActivitySimple {

    TextView titleLeftTv;
    TextView headerTitle;
    RecyclerView notelist;
    SwipeRefreshLayout srl;
    LinearLayout skinbg;
    private String statDate;
    private Adapter adapter;
    private ResCording1 beans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        notelist = findViewById(R.id.notelist);
        srl = findViewById(R.id.srl);
        skinbg = findViewById(R.id.skinbg);
        headerTitle.setText("下注详情");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        statDate = getIntent().getStringExtra("statDate");
        initData();
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        notelist.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
    }

    private void initData() {

        HttpJsonUtils.getHttpApiInstance()
                .getrecrd1(HttpAdress.getuseAdress() + "api/m/user/getStatBetsDetail",Info.user.token, statDate)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCording1>() {
                    @Override
                    protected void onCustomNext(ResCording1 resCording1) {
                        if (resCording1.code == 0 && resCording1.data != null) {
                            srl.setRefreshing(false);
                            beans = resCording1;
                            notelist.setAdapter(adapter);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });


    }

    @Override
    protected int setContentView() {
        return R.layout.activity_recrd1;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.note_itemsecond, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.text1.setText(beans.data.get(position).game_name);
            holder.text2.setText(beans.data.get(position).betCount);
            holder.text3.setText(beans.data.get(position).sumBetMoney + "");
            holder.text4.setText(beans.data.get(position).sumBunko + "");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!beans.data.get(position).betCount.equals("0")) {
                        //跳转新的界面
                        Intent intent = new Intent(RecrdActivity1.this, NoteActivitySecond.class);
                        intent.putExtra("gameid", beans.data.get(position).gameID);
                        intent.putExtra("statDate", statDate);
                        Log.e("xdh",beans.data.get(position).gameID+"----------"+statDate);
                        startActivity(intent);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return beans.data.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView text1, text2, text3, text4;

            MViewHolder(View itemView) {
                super(itemView);
                text1 = (TextView) itemView.findViewById(R.id.text1);
                text2 = (TextView) itemView.findViewById(R.id.text2);
                text3 = (TextView) itemView.findViewById(R.id.text3);
                text4 = (TextView) itemView.findViewById(R.id.text4);
            }
        }
    }
}
